//
//  WBNetworktool.swift
//  weibo
//
//  Created by  郭航 on 16/6/26.
//  Copyright © 2016年 郭航. All rights reserved.
//

//相信你不相信的，不相信你相信的

import UIKit
import AFNetworking

typealias successClosure = (response: AnyObject?) -> Void
typealias failureClosure = (error: AnyObject?) -> Void

enum WBNetworktoolType: String {
    case GET = "GET"
    case POST = "POST"
}

class WBNetworktool: AFHTTPSessionManager {
    
    static let shareNetworktool: WBNetworktool = {
       let networktool = WBNetworktool()
        
        networktool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return networktool
    }()
    
    func request(type: WBNetworktoolType, URLString: String, parameters: AnyObject?, success: successClosure, failure: failureClosure) -> Void {
        if type == .GET {
            GET(URLString, parameters: parameters, progress: nil, success: { (_, response) in
                success(response: response)
                }, failure: { (_, error) in
                    failure(error: error)
            })
        } else {
            POST(URLString, parameters: parameters, progress: nil, success: { (_, response) in
                success(response: response)
                }, failure: { (_, error) in
                    failure(error: error)
            })
        }
    }
 
}

extension WBNetworktool {
    // MARK: - 获取AccessToken
    func oAuthGetAccessToken(code: String, success: successClosure, failure: failureClosure) -> Void {
        let parameters = [
            "client_id" : APPKey,
            "client_secret" : APPSecret,
            "grant_type" : "authorization_code",
            "code" : code,
            "redirect_uri" : APPRedirect_uri
        ]
        
        request(.POST, URLString: "https://api.weibo.com/oauth2/access_token", parameters: parameters, success: success, failure: failure)        
    }
    
    // MARK: - 获取用户信息
    func oAuthGetUserInfor(userInforModel: WBUserInforModel, success: successClosure, failure: failureClosure) -> Void {
        let parameters: [String : AnyObject] = [
            "access_token" : userInforModel.access_token!,
            "uid" : userInforModel.uid!
        ]
        
        request(.GET, URLString: "https://api.weibo.com/2/users/show.json", parameters: parameters, success: success, failure: failure)
    }

    // MARK: - 加载首页数据
    func GetFristPageData(since_id since_id: Int64, max_id: Int64, success: successClosure, failure: failureClosure) -> Void {
        let parameters: [String : AnyObject] = [
            "access_token" : WBUserInfor.shareWBUserInfor.access_token!,
            "since_id" : "\(since_id)",
            "max_id" : "\(max_id)"
        ]
        
        request(.GET, URLString: "https://api.weibo.com/2/statuses/home_timeline.json", parameters: parameters, success: success, failure: failure)
    }
    
    // MARK: - 发布一条微博信息
    func putOutText(status: String, success: successClosure, failure: failureClosure) -> Void {
        let parameters: [String : AnyObject] = [
            "access_token" : WBUserInfor.shareWBUserInfor.access_token!,
            "status" : status
        ]
        
        request(.POST, URLString: "https://api.weibo.com/2/statuses/update.json", parameters: parameters, success: success, failure: failure)
    }
    
    // MARK: - 发布一条带图片的微博
    func putOutImage(status: String, imageArr: [UIImage], success: successClosure, failure: failureClosure) -> Void {
        let URLStr = "https://upload.api.weibo.com/2/statuses/upload.json"
        
        let parameters: [String : AnyObject] = [
            "access_token" : WBUserInfor.shareWBUserInfor.access_token!,
            "status" : status
        ]
        
        POST(URLStr, parameters: parameters, constructingBodyWithBlock: { (fromData) -> Void in
            
            for image in imageArr {
                let data = UIImagePNGRepresentation(image)
                fromData.appendPartWithFileData(data!, name: "pic", fileName: "", mimeType: "application/octet-stream")
            }
            
            }, progress: nil, success: { (_, response) -> Void in
                success(response: response)
            }) { (_, error) -> Void in
                failure(error: error)
        }
    }
    
}
